//
//  CYYNetWorkManager+PartyState.m
//  LetsPartyBmob
//
//  Created by rimi on 15/9/10.
//  Copyright (c) 2015年 柴阳阳. All rights reserved.
//

#import "CYYNetWorkManager+PartyState.h"

@implementation CYYNetWorkManager (PartyState)
//活动状态
+ (void)partyStateWithPartyObjectId:(NSString *)partyObjectId
                             sucess:(Sucess)sucess
                               fail:(Fail)fail{
    //请求下来活动的基本信息（关注着，参加者，时间，有无发起者）
    [CYYNetWorkManager partyDetailWithObjectId:partyObjectId sucess:^(BmobObject *object) {
        //判断是否过期
        if ([[object objectForKey:@"partyEndTime"] compare:[CYYNetWorkManager tStringFromDate:[NSDate date]]] == -1) {
            sucess(@"已过期");
        }else if ([[object objectForKey:@"partyState"]  isEqual: @"已取消"]) {
            //首先判断有没有发起者--【已取消】
            sucess(@"已取消");
        }else{
            NSMutableArray * objectIdArray = [NSMutableArray array];
            //请求活动的参加者
            [CYYNetWorkManager partyParticipatorsListWithPartyObjectId:partyObjectId sucess:^(NSArray *array) {
                for (BmobUser * bUser in array) {
                    [objectIdArray addObject:bUser.objectId];
                }
                NSPredicate * predice = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",[CYYNetWorkManager currentUserGet].objectId];
                [objectIdArray filteredArrayUsingPredicate:predice].count != 0 ? sucess(@"已参加") : [CYYNetWorkManager isObserveThisPartyWithPartyObjectId:partyObjectId sucess:^(NSString *sucessString) {
                    sucess(sucessString);
                } fail:^(NSError *error) {
                    fail(error);
                }];
            } fail:^(NSError *error) {
                fail(error);
            }];
        }
    } fail:^(NSError *error) {
        fail(error);
    }];
}

//判断是否关注了该活动
+ (void)isObserveThisPartyWithPartyObjectId:(NSString *)partyObjectId
                                     sucess:(Sucess)sucess
                                       fail:(Fail)fail{
    NSMutableArray * objectIdArray = [NSMutableArray array];
    //请求活动的关注着
    [CYYNetWorkManager observerListWithPartyObjectId:partyObjectId sucess:^(NSArray *array) {
        for (BmobUser * bUser in array) {
            [objectIdArray addObject:bUser.objectId];
        }
        NSPredicate * predice = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",[CYYNetWorkManager currentUserGet].objectId];
        [objectIdArray filteredArrayUsingPredicate:predice].count == 0 ? sucess(@"未关注"):sucess(@"已关注");
    } fail:^(NSError *error) {
//        NSLog(@"%@",error);
    }];
}



@end
